<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <base th:href="${#httpServletRequest.getContextPath()} + '/'">
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="layui/css/layui.css">
    <!-- 引入 layui.js -->
    <script src="layui/layui.js"></script>
</head>
<body style="width: 99%">
<div style="width: 630px">
    <div>
        <div class="layui-card">
            <div class="layui-card-body layui-bg-gray">
                <form class="layui-form" lay-filter="addUserForm">
                    <div class="layui-form-item">
                        <label class="layui-form-label">手机</label>
                        <div class="layui-input-inline" style="width: 350px">
                            <label>
                                <input name="phone" placeholder="请输入手机号码" lay-verify="phone"
                                       autocomplete="off"
                                       class="layui-input">
                            </label>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary" id="getVerifyCode">获取验证码</button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">验证码</label>
                        <div class="layui-input-block">
                            <label>
                                <input name="verifyCode" placeholder="请输入验证码" lay-verify="verifyCode"
                                       autocomplete="off"
                                       class="layui-input">
                            </label>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <label>
                                <input type="password" name="password" placeholder="请输入密码" lay-verify="password"
                                       autocomplete="off"
                                       class="layui-input">
                            </label>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">确认密码</label>
                        <div class="layui-input-block">
                            <label>
                                <input type="password" placeholder="请再次输入密码" lay-verify="confirmPass"
                                       autocomplete="off"
                                       class="layui-input">
                            </label>
                        </div>
                    </div>
                    <div class="layui-btn-container" style="width: auto;margin-left: 435px">
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        <button type="submit" class="layui-btn" style="margin-right: 0" lay-submit=""
                                lay-filter="addUserSubmit">提交
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        const form = layui.form
            , layer = layui.layer
            , $ = layui.$;

        //自定义验证规则
        form.verify({
            password: [
                /^[\S]{6,16}$/
                , '密码必须6到16位，且不能出现空格'
            ]
            ,
            confirmPass: function (value) {
                if ($('input[name=password]').val() !== value)
                    return '两次密码输入不一致！';
            }
            , phone: [
                /^1[3456789][0-9]{9}$/
                , '手机号码格式错误'
            ]
            , verifyCode: [
                /^[0-9]{6}$/
                , '验证码错误'
            ]
        });

        //监听提交
        form.on('submit(addUserSubmit)', function (data) {
            $.ajax({
                url: "user/change/password",
                type: "post",
                data: JSON.stringify(data.field),
                contentType: "application/json;charset=utf8",
                dataType: "json",
                success: function (response) {
                    let code = response.code;
                    if (code == 0) {
                        layer.msg("修改成功! 3s后跳转到登录页面", {icon: 1, offset: "auto", time: 3000});
                        setTimeout(function () {
                            let index = parent.layer.getFrameIndex(window.name); // 获取当前窗口的name
                            parent.layer.close(index);
                        }, 3000);
                    } else {
                        layer.msg("修改失败! " + response.code + " " + response.msg, {icon: 0, offset: "auto", time: 2000});
                    }
                },
                error: function (response) {
                    layer.msg("修改失败! " + response.status + " " + response.statusText, {
                        icon: 0,
                        offset: "auto",
                        time: 2000
                    });
                }
            });
            return false;
        });

        $("#getVerifyCode").click(function () {
            let reg = /^1[3456789][0-9]{9}$/;
            let phone = $("input[name=phone]").val();
            if (!reg.test(phone)) {
                layer.msg("手机号码格式错误", {icon: 0, offset: "auto", time: 2000});
                return;
            }
            let result = -1;
            $.ajax({
                url: "user/send/message/verify/code",
                type: "post",
                data: {
                    phone: phone
                },
                async: false,
                dataType: "json",
                success: function (response) {
                    let code = response.code;
                    if (code == 0) {
                        layer.msg("发送成功!", {icon: 1, offset: "auto", time: 2000});
                        result = 0;
                    } else {
                        layer.msg("发送失败! " + response.code + " " + response.msg, {icon: 0, offset: "auto", time: 2000});
                    }
                },
                error: function (response) {
                    layer.msg("发送失败! " + response.status + " " + response.statusText, {
                        icon: 0,
                        offset: "auto",
                        time: 2000
                    });
                }
            })
            if (result !== 0) {
                return;
            }
            let that = $(this);
            let time = 60;
            let timeStop = setInterval(function () {
                if (--time > 0) {
                    that.text('重新发送' + time + 's');
                    that.addClass("layui-btn-disabled").attr("disabled", true)
                } else {
                    time = 60;  // 当减到0时赋值为60
                    that.text('获取验证码');
                    clearInterval(timeStop); // 清除定时器
                    that.removeClass("layui-btn-disabled").attr("disabled", false);    // 移除属性，可点击
                }
            }, 1000)
        });
    });
</script>
</html>